package org.star.dolphin.config;

import org.star.dolphin.domain.RedisProperties;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * Redisson https://github.com/redisson/redisson/wiki
 */
@Configuration
@ConditionalOnProperty(prefix = "redis", name = "database")
public class RedisConfig {

    @Autowired
    private RedisProperties rp;

    @Bean
    public RedissonClient redissonClient() throws IOException {
        Config config = new Config();
        SingleServerConfig singleConfig = config.useSingleServer();

        singleConfig.setAddress("redis://" + rp.getHost() + ":" + rp.getPort());
        singleConfig.setDatabase(rp.getDatabase());
        singleConfig.setPassword(rp.getPassword());

        //更改对象编码为String
        config.setCodec(JsonJacksonCodec.INSTANCE);

        return Redisson.create(config);
    }

}
